package com.zzx.threekingdom.card;


import com.zzx.threekingdom.resmanager.ResManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

public class Card
{
    public int mResId = -1;
    public boolean upsideDown = false;
    public Bitmap mBitmap = null;
    public String name = null;
    public String helpText = null;

    public enum CardType
    {
        empty, identity, character, base, knowhow, equipment;
    }
    
    public CardType mCardType = CardType.empty;
    
    public void setBitmap(Bitmap map){ mBitmap = map; }
    public Bitmap getBitmap(){ return mBitmap; }
    public void setName(String n) { name = n; }
    public String getName() { return name; }
    public void setRedId(int id) { mResId = id; }
    public int getRedId() { return mResId; }
    public void setUpsideDown(boolean d) { upsideDown = d; }
    public boolean getUpsideDown() { return upsideDown; }
    public void setTextAndMap(String text) { setName(text);setBitmap(ResManager.getBitmap(text)); }
    public void setHelpText(String text) { helpText = text; }
    public String getHelpText() { return helpText; }
    
    
    public CardType getCardType()
    {
        if(this instanceof IdentityCard)
            return CardType.identity;
        else if(this instanceof CharacterCard)
            return CardType.character;
        else if(this instanceof BaseCard)
            return CardType.base;
        else if(this instanceof KnowhowCard)
            return CardType.knowhow;
        else if(this instanceof EquipmentCard)
            return CardType.equipment;

        return CardType.empty;
    }
   
    public Bitmap getBitmap(Resources res)
    {
        if(upsideDown==false)
        {
            if(mBitmap!=null)
                return mBitmap;

            if(mResId!=-1)
            {
                Options options = new Options();
                options.inPurgeable = true;
                options.inDither = false;
                return (mBitmap = BitmapFactory.decodeResource(res, mResId, options));
            }
        }
        upsideDown = true;
        return ResManager.getBitmap("back");
    }
    
    public static boolean isCardValid(Card card)
    {
        if((card==null)||(card.getCardType() == CardType.empty))
            return false;
        return true;
    }
    
    public static String getCardDescription(Card card)
    {
        if(card!=null)
        {
            if(card instanceof PatternCard)
            {
                return ((PatternCard)card).getPatternText();
            }
            return card.getName();
        }
        return null;
        
    }
    
    public static boolean isCardByName(Card card, String name) { return ((card != null)&&card.getName().equals(name)); }
    

}
